import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_taotaoshipin/util/global.dart';
import 'package:flutter_taotaoshipin/value/sp_key.dart';
import 'package:flutter_taotaoshipin/widget/my_app_bar.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:get/get.dart';

class DebugPage extends StatefulWidget {
  const DebugPage({super.key});

  @override
  State<DebugPage> createState() => _DebugPageState();
}

class _DebugPageState extends State<DebugPage> {
  TextEditingController textEditingController = TextEditingController();

  @override
  void dispose() {
    textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(centerTitle: '测试'),
      body: ListView(
        children: [
          ListTile(
            title: Text('订阅退款'),
            trailing: Icon(Icons.chevron_right_rounded),
            onTap: () async {
              var res = await Get.dialog(
                AlertDialog.adaptive(
                  title: Text('退款申请'),
                  content: Padding(
                    padding: const EdgeInsets.only(top: 15),
                    child: CupertinoTextField(
                      controller: textEditingController,
                      placeholder: '请输入苹果订单号',
                      textInputAction: TextInputAction.done,
                      keyboardType: TextInputType.number,
                    ),
                  ),
                  actions: [
                    TextButton(
                      onPressed: () {
                        Get.back();
                      },
                      child: Text('取消'),
                    ),
                    TextButton(
                      onPressed: () async {
                        Get.back();
                        // try {
                        //   var transactionId =
                        //       int.parse(textEditingController.text);
                        //   var message = await HiStorekitHelper()
                        //       .beginRefundRequest(transactionId);
                        //   EasyLoading.showToast(message ?? '返回结果为空');
                        // } catch (_) {
                        //   EasyLoading.showError('请输入纯数字');
                        // }
                      },
                      child: Text('确定'),
                    ),
                  ],
                ),
                barrierDismissible: false,
              );
            },
          ),
          ListTile(
            title: Text('显示钥匙串账号'),
            trailing: Icon(Icons.chevron_right_rounded),
            onTap: () async {
              var userInfo = await Global.getLocalUserInfoKeychain();
              var res = await Get.dialog(
                AlertDialog.adaptive(
                  title: Text('显示钥匙串账号'),
                  content: Padding(
                    padding: const EdgeInsets.only(top: 15),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('账号：${userInfo.user}'),
                        Text('密码：${userInfo.password}'),
                        Text('mid：${userInfo.mId}'),
                        Text('sessionId：${userInfo.sessionId}'),
                      ],
                    ),
                  ),
                  actions: [
                    TextButton(
                      onPressed: () {
                        Get.back();
                      },
                      child: Text('取消'),
                    ),
                    TextButton(
                      onPressed: () async {
                        Get.back();
                        try {
                          var str = await const FlutterSecureStorage()
                              .read(key: SpKey.localUserInfo);
                          Clipboard.setData(ClipboardData(text: str ?? ''));
                          EasyLoading.showSuccess('复制成功');
                        } catch (e) {
                          EasyLoading.showError('复制失败');
                        }
                      },
                      child: Text('复制'),
                    ),
                  ],
                ),
                barrierDismissible: false,
              );
            },
          ),
          ListTile(
            title: Text('删除钥匙串账号'),
            trailing: Icon(Icons.chevron_right_rounded),
            onTap: () async {
              var res = await Get.dialog(
                AlertDialog.adaptive(
                  title: Text('删除钥匙串账号'),
                  content: Padding(
                    padding: const EdgeInsets.only(top: 15),
                    child: Text('删除钥匙串账号'),
                  ),
                  actions: [
                    TextButton(
                      onPressed: () {
                        Get.back();
                      },
                      child: Text('取消'),
                    ),
                    TextButton(
                      onPressed: () async {
                        Get.back();
                        try {
                          await const FlutterSecureStorage()
                              .delete(key: SpKey.localUserInfo);
                          await SpUtil.remove(SpKey.localUserInfo);
                          EasyLoading.showSuccess('删除成功');
                        } catch (e) {
                          EasyLoading.showError('删除失败');
                        }
                      },
                      child: Text('确定'),
                    ),
                  ],
                ),
                barrierDismissible: false,
              );
            },
          ),
        ],
      ),
    );
  }
}
